package com.dayatang.android.Adapter;

import java.util.HashMap;
import java.util.List;
import java.util.concurrent.ExecutionException;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.dayatang.android.Activity.R;
import com.dayatang.android.Db.SystemMessageServiceDb;
import com.dayatang.android.Model.SystemMessage;
import com.dayatang.android.Task.BaseTask;
import com.dayatang.android.Util.BitmapUtil;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MyGridViewAdapter extends BaseAdapter {

	private Context context;
	private List<HashMap<String,Object>> list;
	private LayoutInflater listContainer; 
	private String serverAddress;
	
	private String smNum;
	
	private SystemMessageServiceDb systemMessageServiceDb;
	
	public MyGridViewAdapter(Context context, List<HashMap<String, Object>> list, String serverAddress) {
		this.context = context;
		this.list = list;
		this.serverAddress = serverAddress;
		listContainer = LayoutInflater.from(context);
		this.systemMessageServiceDb = new SystemMessageServiceDb(context);
		InitData();
		smNum = String.valueOf(systemMessageServiceDb.findSM("already_read=?", new String[]{"0"}).size());
	}
	
	class ItemView {
		private ImageView gridViewItemImageView;
		private TextView gridViewItemTextView;
	}
	
	public int getCount() {
		return list.size();
	}

	public Object getItem(int position) {
		return list.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {

		ItemView itemView = null;
		
		if(convertView == null) {
			itemView = new ItemView();
			convertView = listContainer.inflate(R.layout.gridview_item, null);   
			
			itemView.gridViewItemImageView = (ImageView) convertView.findViewById(R.id.gridview_itemImage);
			itemView.gridViewItemTextView = (TextView) convertView.findViewById(R.id.gridview_item_name);
			
			convertView.setTag(itemView);
		} else {
			itemView = (ItemView) convertView.getTag();
		}
		
		if (((String)list.get(position).get("ItemName")).equals(context.getString(R.string.index_system_message))) {
			Log.i("123", smNum);
			Bitmap bitmap = BitmapUtil.CreateMyTaskCountIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.system_message_icon), context,smNum);
			itemView.gridViewItemImageView.setImageBitmap(bitmap);
			itemView.gridViewItemTextView.setText((String)list.get(position).get("ItemName"));
		} else {
			itemView.gridViewItemImageView.setImageResource((Integer) list.get(position).get("ItemImage"));
			itemView.gridViewItemTextView.setText((String)list.get(position).get("ItemName"));
		}
		
		return convertView;
	}

	private void InitData() {
		
		String url = serverAddress + "/remote/system-message.action?newid=" + systemMessageServiceDb.getNewId();

		try {
			String result = new BaseTask(context).execute(url).get();
			
			if (result.equals("error")) {
				Toast.makeText(context,context.getString(R.string.serverAddress_alert_addressError),Toast.LENGTH_LONG).show();
			}  else if (result.length() > 10) {
				
				JSONArray jsonArray = new JSONArray(result);
				
				for(int i = 0,len = jsonArray.length(); i < len;i++){
					
					JSONObject jsonObject = jsonArray.getJSONObject(i);
					
					SystemMessage systemMessage = new SystemMessage();
					systemMessage.setThisid(jsonObject.getString("thisid"));
					systemMessage.setTitle(jsonObject.getString("title"));
					systemMessage.setContent(jsonObject.getString("content"));
					systemMessage.setOrganizationName(jsonObject.getString("organizationName"));
					systemMessage.setReleaseDate(jsonObject.getString("releaseDate"));
					systemMessage.setAlreadyRead("0");
					
					systemMessageServiceDb.save(systemMessage);
					
				}
				systemMessageServiceDb.updateNewestId(jsonArray.getJSONObject(jsonArray.length()-1).getString("thisid"));
				
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		} catch (JSONException e) {
			e.printStackTrace();
		}
		
	}
	
}
